using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace WindowsGame.Menus
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class StartMenuComponent : Microsoft.Xna.Framework.GameComponent
    {
        GameStateComponent gameState;

        SpriteFont font1, font2;

        int windowHeight, windowWidth;

        //For controlling camera
        bool moveCameraLeft;

        public StartMenuComponent(Game game, GameStateComponent gameState, int windowHeight, int windowWidth)
            : base(game)
        {
            //Inherit game state component
            this.gameState = gameState;

            this.windowWidth = windowWidth;
            this.windowHeight = windowHeight;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            moveCameraLeft = false;

            base.Initialize();
        }

        public void LoadContent(ContentManager Content)
        {
            font1 = Content.Load<SpriteFont>("Fonts\\Font");
            font2 = Content.Load<SpriteFont>("Fonts\\Font2");
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Update(GameTime gameTime, Camera2DComponent camera, MapComponent map, CollectibleComponents.CollectibleSpawnerComponent spawner)
        {
            //Get keyboard state to get user input
            KeyboardState keyState = Keyboard.GetState();

            if (keyState.IsKeyDown(Keys.Enter))
            {
                gameState.SetGameState(GameStateComponent.GameStates.Play); //Change gamestate to play
                map.changeMap("map1_1"); //Change map to first map
                spawner.SpawnFromBounds(); //Use method to spawn collectibles
            }


            //See whether camera shall move left or right
            if (camera.getPosition().X + windowWidth >= map.getMapDimensions().X - 64)
                moveCameraLeft = true;
            if (camera.getPosition().X <= 64)
                moveCameraLeft = false;

            //Update camera movement
            if (!moveCameraLeft)
                camera.setPosition(new Vector2(camera.getPosition().X + 1, camera.getPosition().Y));
            else
                camera.setPosition(new Vector2(camera.getPosition().X - 1, camera.getPosition().Y));


            base.Update(gameTime);
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();

            spriteBatch.DrawString(font1, "The Trash Game", new Vector2(windowWidth / 2 - font1.MeasureString("The Trash Game").X / 2, windowHeight / 4), Color.White);
            spriteBatch.DrawString(font2, "Press ENTER to play", new Vector2(windowWidth / 2 - font2.MeasureString("Press ENTER to play").X / 2, windowHeight / 3), Color.White);

            spriteBatch.End();
        }
    }
}
